package com.alex.xmlyfm.fragments;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alex.xmlyfm.R;
import com.alex.xmlyfm.adapters.CommonFragmentPagerAdapter;
import com.alex.xmlyfm.fragments.discover.DiscoverAnchorFragment;
import com.alex.xmlyfm.fragments.discover.DiscoverLiveFragment;
import com.alex.xmlyfm.fragments.discover.DiscoverRatingFragment;
import com.alex.xmlyfm.fragments.discover.DiscoverRecommendFragment;
import com.alex.xmlyfm.fragments.discover.DiscoverCategoryFragment;

import java.util.LinkedList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class DiscoverFragment extends Fragment implements TabLayout.OnTabSelectedListener {


    private ViewPager viewPager;

    public DiscoverFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view = inflater.inflate(R.layout.fragment_discover, container, false);

        TabLayout tabLayout = (TabLayout) view.findViewById(R.id.discover_top_bar);


        List<BaseFragment> fragments = new LinkedList<>();
        fragments.add(new DiscoverRecommendFragment());
        fragments.add(new DiscoverCategoryFragment());
        fragments.add(new DiscoverLiveFragment());
        fragments.add(new DiscoverRatingFragment());
        fragments.add(new DiscoverAnchorFragment());

        // viewPager 加载
        viewPager = (ViewPager) view.findViewById(R.id.discover_view_pager);

        CommonFragmentPagerAdapter pagerAdapter = new CommonFragmentPagerAdapter(getChildFragmentManager(),fragments);

        viewPager.setAdapter(pagerAdapter);
        tabLayout.setupWithViewPager(viewPager);
        // viewPager 滑动与 TabLayout 绑定
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));


        tabLayout.setOnTabSelectedListener(this);


        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


    }


    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
}
